package com.tanhua.server.controller;


import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ck
 * @date 2021/11/11 21:39
 */

@RestController
@RequestMapping("/users")
public class SettingsController {


    @Autowired
    private SettingsService settingsService;

    /**
     * 查寻通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1、获取参数
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置通知
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        //获取参数
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        //1、调用service查询
        PageResult pr = settingsService.blacklist(page,pageSize);
        //2、构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     * @param blackUserId
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-1、发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //1、调用service获取验证码
        settingsService.getVerificationCode();

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-2、校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        String mobile = UserHolder.getMobile();
        Map<String, Boolean> stringBooleanMap = settingsService.checkVerificationCode(mobile,verificationCode);
        return ResponseEntity.ok(stringBooleanMap);
    }

    /**
     * 修改手机号-3、保存
     */

    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        settingsService.updateWithPhone(phone);
        return ResponseEntity.ok(null);
    }
}
